package com.nd.android.jsondemo;

import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Jsondemo extends Activity {
    /** Called when the activity is first created. */ 
	//http://api.douban.com/movie/subject/2277018?alt=json
    
    int num=0;
    String[] str ={"2277018", "3412882", "1433330", "3683409", "2357162"}; 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        update(num);

    }
    
    public void update(int i){
        Douban api = new Douban();
        JSONArray jArr;  
        JSONObject jobj;
        U.dout(i);
        try {
        	jobj = api.getMovie(str[i]);
			//JSONObject jo = jArr.getJSONObject(0);
        	JSONObject jj = (JSONObject)jobj.get("title");//取电影名        	
            TextView movie_name = (TextView)findViewById(R.id.movie_name);
            U.dout(jj.getString("$t"));
            movie_name.setText(jj.getString("$t"));
            
            JSONObject jt = (JSONObject)jobj.get("summary");//取电影简介
            TextView intro = (TextView)findViewById(R.id.intro);
            intro.setText(jt.getString("$t"));

            jArr = (JSONArray)jobj.get("link");//取电影图片 
            JSONObject jp = (JSONObject)jArr.get(2);
            String url = jp.getString("@href");
            ImageView img = (ImageView)findViewById(R.id.pic);
            img.setImageBitmap(U.returnBitMap(url));
		} catch (Exception e) {			
			e.printStackTrace(); 
		}

    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
//		menu.add(0, Menu.FIRST, 0, "上一页");
//		menu.add(0, Menu.FIRST + 1, 1, "下一页");
//		menu.add(0,Menu.FIRST + 2,2,"Build Time");
		this.getMenuInflater().inflate(R.menu.navigator, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		U.dout(item.getItemId());
		switch (item.getItemId()) {		
		case R.id.previous:			
			if(num==0){
				U.dout("000");
				break;
			}
			update(--num);
			break;
		case R.id.next:				
			if(num==4){
				U.dout("444");
				break;
			}
			update(++num);
			break;
		case R.id.buildTime:
			Toast.makeText(getApplicationContext(), "Build Time -> "+new Date(Build.TIME).toString(),Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
